<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <base href="<%=request.getContextPath()%>/">
    <link rel="stylesheet" href="static/plugins/layui/css/layui.css">
    <script src="static/plugins/jquery-3.1.0.js"></script>
    <script src="static/plugins/layui/layui.js"></script>
    <style>
        .required {
            color: red;
            margin: 0 5px;
        }
    </style>
</head>
<body style="padding: 15px">
<div class="layui-container">
    <form class="layui-form" id="dataForm" lay-filter="dataForm">
        <div class="layui-form-item">
            <label for="name" class="layui-form-label"><span class="required">*</span>分类名称</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="name" id="name" placeholder="请输入分类名称"
                       lay-verify="required" lay-reqtext="分类名称不能为空">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="status" class="layui-form-label">
                <span class="required">*</span>
                状态
            </label>
            <div class="layui-input-block">
                <select name="status" id="status" lay-verify="required" lay-reqtext="请选择状态">
                    <option value="1">可用</option>
                    <option value="2">禁用</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="profile" class="layui-form-label">简介</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" name="profile" id="profile"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="save">提交</button>
            </div>
        </div>
    </form>
</div>
</body>
<script>
    var form;
    /*判断是否为修改*/
    const dataId = sessionStorage.getItem("dataId") || "";
    $(function () {
        layui.use(function () {
            form = layui.form;
            getFormData()
            form.on("submit(save)", function ({field}) {
                $.ajax({
                    url: "category/update",
                    data: {
                        id: dataId,
                        ...field
                    },
                    method: "post",
                    dataType: "json",
                    success(res) {
                        if (res.code === 200) {
                            layer.msg('操作成功！', {icon: 6}, function () {
                                let index = parent.layer.getFrameIndex(window.name); // 先得到当前 iframe 层的索引
                                parent.layer.close(index); // 再执行关闭
                                /*刷新list页面的table*/
                                parent.table.reload('dataTable');
                            });
                        } else {
                            layer.alert(res.msg)
                        }
                    }
                });
                return false;
            });
        })
    })

    function getFormData() {
        if (dataId) {
            /*获取后立即删除*/
            sessionStorage.removeItem("dataId");
            $.ajax({
                url: "category/info/" + dataId,
                method: "get",
                success(res) {
                    if (res.code === 200) {
                        form.val('dataForm', res.data);
                    }
                }
            })
        }
    }
</script>
</html>
